<template>
  <div class="app-container">
    <div>
      <el-form
        :model="userForm"
        label-position="right"
        label-width="130px"
        ref="userForm"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="姓名:" prop="name">
              <el-input
                v-model="userForm.name"
                placeholder="请输入内容"
                size="small"
                style="width: 150px"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="身份证号:" prop="idCardNumber">
              <el-input
                v-model="userForm.idCardNumber"
                placeholder="请输入内容"
                size="small"
                style="width: 150px"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="手机号:" prop="phone">
              <el-input
                v-model="userForm.phone"
                placeholder="请输入内容"
                size="small"
                style="width: 150px"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="年龄从:" prop="ageFrom">
              <el-input
                v-model="userForm.ageFrom"
                placeholder="请输入内容"
                size="small"
                style="width: 150px"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="至:" prop="ageTo">
              <el-input
                v-model="userForm.ageTo"
                placeholder="请输入内容"
                size="small"
                style="width: 150px"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别:" prop="sex">
              <el-select
                v-model="userForm.sex"
                placeholder="请选择"
                style="width: 150px"
              >
                <el-option
                  v-for="item in sex_options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-form-item>
            <el-button
              type="success"
              icon="plus"
              style="width: 80px"
              @click="searchByUser"
              >筛选</el-button
            >
            <!-- v-if="hasPerm('strategy:auto')" -->
            <el-button
              type="danger"
              icon="plus"
              style="width: 80px"
              @click="resetsearchByUser"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <el-table
      :data="userTable"
      v-loading.body="listLoading"
      element-loading-text="拼命加载中"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        align="center"
        label="id"
        prop="id"
        width="150"
        v-if="false"
      ></el-table-column>
      <el-table-column
        align="center"
        label="姓名"
        prop="name"
        width="150"
      ></el-table-column>
      <el-table-column
        align="center"
        label="手机号"
        prop="phone"
        width="150"
      ></el-table-column>
      <el-table-column
        align="center"
        label="身份证号"
        prop="idCardNumber"
        width="200"
      ></el-table-column>
      <el-table-column
        align="center"
        label="年龄"
        prop="age"
        width="150"
      ></el-table-column>
      <el-table-column
        align="center"
        label="性别"
        prop="sex"
        width="200"
      ><template slot-scope="scope">
        <span v-if="scope.row.sex=='MAN'">男</span>
        <span v-else>女</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="edit"
            @click="editUser(scope.row)"
            >编辑</el-button
          >
          <!-- v-if="hasPerm('user:update')" -->
           <!-- v-if="scope.row.orderStatus == 'EFFECTIVE'" -->
          <!-- v-if="hasPerm('user:update')" -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleUserSizeChange"
      @current-change="handleCurrentUserChange"
      :current-page="listQuery.pageNum"
      :page-size="listQuery.pageRow"
      :total="totalUserCount"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <el-dialog
      title="用户编辑"
      :visible.sync="dialogUserVisible"
      style="min-width: 800px"
    >
      <el-form
        :model="userDetailForm"
        :rules="userDetailFormRules"
        label-position="left"
        style="margin-left: 20px"
        ref="userDetailForm"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="姓名:"
              label-width="110px"
              style="width: 300px"
              prop="name"
            >
             <el-input v-model="userDetailForm.name" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="身份证号:"
              label-width="110px"
              style="width: 300px"
              prop="idCardNumber"
            >
            <el-input v-model="userDetailForm.idCardNumber" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="手机号:"
              label-width="110px"
              style="width: 300px"
              prop="phone"
            >
             <el-input v-model="userDetailForm.phone" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
       
       <el-row type="flex" justify="center">
          <el-form-item>
            <el-button
              type="success"
              icon="plus"
              style="width: 80px"
              @click="saveUser()"
              :loading="loading"
              >保存</el-button
            >
            <!-- v-if="hasPerm('strategy:auto')" -->
            <el-button
              type="danger"
              icon="plus"
              style="width: 80px"
              @click="close()"
              >取消</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      totalUserCount: 0, //分页组件--数据总条数
      userTable: [], //表格的数据
      listLoading: false, //数据加载等待动画
      listLoadingDialog: false,
      loading: false,
      listQuery: {
        pageNum: 1, //页码
        pageRow: 10, //每页条数
      },

      activeName: "first",
      dialogStatus: "details",
      dialogUserVisible:false,
      userForm: {
        name: "",
        phone: "",
        idCardNumber: "",
        ageFrom: "",
        ageTo: "",
        sex: ""
      },
       userDetailForm: {
        id:"",
        name: "",
        phone: "",
        idCardNumber: ""
      },
      sex_options: [
        {
          value: "male",
          label: "男",
        },
        {
          value: "female",
          label: "女",
        }
      ],

      userDetailFormRules: {
        name: [
          { required: true, trigger: "blur", message: "请输入姓名" }
        ],
        phone: [{ required: true, trigger: "blur", message: "请输入手机号" }],
         idCardNumber: [{ required: true, trigger: "blur", message: "请输入身份证号" }],
      },
    };
  },
  created() {
    this.searchByUser();
  },
  filters: {
    checkStatus: function (status, list) {
      let obj = list.find((item) => item.value === status);
      if (obj) {
        return obj.label ? obj.label : "";
      } else {
        return "";
      }
    },
  },
  methods: {
    searchByUser() {
      //查询列表
      let url =
        "/user/query/" + this.listQuery.pageNum + "/" + this.listQuery.pageRow;
      this.listLoading = true;
      let data = {};

      data = {
        name: this.userForm.name,
        phone: this.userForm.phone,
        idCardNumber: this.userForm.idCardNumber,
        ageFrom: this.userForm.ageFrom,
        ageTo: this.userForm.ageTo,
        sex: this.userForm.sex
      };
      this.api({
        url: url,
        method: "post",
        data: data,
      }).then((data) => {
        this.listLoading = false;
        this.userTable = data.data.datas;
        this.totalUserCount = data.data.total;
      });
    },
    resetsearchByUser() {
      this.$refs.userForm.resetFields();
    },
     handleUserFilter() {
      this.listQuery.pageNum = 1;
      this.searchByUser();
    },
    handleUserSizeChange(val) {
      //改变每页数量
      this.listQuery.pageRow = val;
      this.handleUserFilter();
    },
    handleCurrentUserChange(val) {
      //改变页码
      this.listQuery.pageNum = val;
      this.searchByUser();
    },

    getIndex($index) {
      //表格序号
      return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1;
    },
  
    editUser(row) {
      this.dialogUserVisible = true
      this.userDetailForm.id = row.id
      this.userDetailForm.name = row.name
      this.userDetailForm.phone = row.phone
      this.userDetailForm.idCardNumber = row.idCardNumber
    },
    saveUser()
    {
        this.$refs.userDetailForm.validate(valid => {
            if (valid) {
                this.loading = true;
                let url = "user/update/"+this.userDetailForm.id;
                let params={
                  id:this.userDetailForm.id,
                  name:this.userDetailForm.name,
                  phone:this.userDetailForm.phone,
                  idCardNumber:this.userDetailForm.idCardNumber
                };
                this.api({
                  url: url,
                  method: "put",
                  data: params,
                }).then((data) => {
                    this.loading = false;
                    if(data.errorMessage)
                    {
                       this.$message.info(data.errorMessage);
                    }
                    this.searchByUser()
                    this.dialogUserVisible=false;
                });
            }
        })
     
    },
    close()
    {
       this.dialogUserVisible=false;
    },

  },
};
</script>
